package com.cn.lp.export.server.base.result;


import com.cn.lp.export.server.base.utils.StringAide;

import java.util.HashMap;
import java.util.Map;

/**
 * 结果码集合
 */
public class ResultCodes {

    private static final Map<Integer, ResultCode> codeMap = new HashMap<>();

    /**
     * 根据ID获取结果
     *
     * @param id
     * @return
     */
    public static ResultCode of(int id) {
        return codeMap.get(id);
    }

    /**
     * 注册结果码，判断是否有重复
     *
     * @param code
     */
    static void registerCode(ResultCode code) {
        ResultCode old = codeMap.put(code.getCode(), code);
        if (old != null) {
            IllegalArgumentException e = new IllegalArgumentException(StringAide.format("{}.{}.{} 与 {}.{}.{} id 都为 {}",
                code.getClass(), code.getRawMessage(), code, old.getClass(), old.getRawMessage(), old, old.getCode()
            ));
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 判断是否重复
     *
     * @param code
     * @return
     */
    public static boolean isSuccess(int code) {
        return code == ResultCode.SUCCESS_CODE;
    }

    /**
     * 判断是否重复
     *
     * @param code
     * @return
     */
    public static boolean isSuccess(ResultCode code) {
        return code.getCode() == ResultCode.SUCCESS_CODE;
    }

}
